/*
 * @Author: guoyangf
 * @Date: 2020-07-03 16:23:18
 * @LastEditTime: 2020-08-11 16:17:03
 * @Description: 查询区自定义字段事件
 */
import { toast } from "nc-lightapp-front";
import requestApi from '../requestApi';

export default function () {
    return {
        barcode: {
            notClearInvokeQuery: this.props.sscAppType == 'sscfile',  //清空时不调用查询
            onChange: () => {
            },
            onSearch: async (barcode) => {
                let tabcode = this.state.activeTabKey;
                let data, result;
                data = result = await requestApi.queryByBarCode({
                    tasktype: this.tasktype,
                    tabcode,
                    barcode
                })


                let tasklist = data.data.tasklist;

                if (!tasklist.rows.length) {
                    toast({ title: this.state.multiLang['job-common-wtqddj'], duration: 5, color: 'danger', }) /*未提取到单据*/
                    return;
                }

                if (this.props.sscAppType == 'sscfile') { //我的打印直接插入 我的打印没有翻页
                    let allRows = this.props.table.getAllTableData(tabcode).rows;
                    let newRow = tasklist.rows[0];
                    let hasRow = allRows.some((row) => {
                        if (row.values.billno.value == newRow.values.billno.value) {
                            return true;
                        }   
                    })
                    
                    if (hasRow) {
                        toast({ title: this.state.multiLang['job-common-djytq'], duration: 5, color: 'danger', }) /*单据已存在*/
                        return ;
                    }

                    let insertPos = 0;
                    if (!this.state.isInsertBefore) {
                        insertPos = allRows.length;
                    }
                    this.props.table.addRow(tabcode, tasklist, insertPos);
                    //更新页签和查询区扩展字段计数
                    this.updateTabAndSearchCount();
                } else { //其他作为精准查询
                    //清空PKList
                    this.currentTabPkList = result.data.taskPkList;;
                    //清除轮询
                    clearInterval(this.pkListRefreshTimer)

                    if (this.state.isAutoOpenBillAfterQueryBarCode) { //自动打开单据，在单据扫描之后
                        this.openBill(tasklist.rows[0].values, 0);
                    }

                    let tabCode = this.state.activeTabKey;

                    let listConfigArr = this.state.listConfigArr;
                    let targetListConfig = listConfigArr.find(config => config.tabCode == tabCode);

                    /* let cardData = this.tableCardDataHandle(null, result.data.taskcard, result.data.tasklist);

                    targetListConfig.firstPageRowData = result.data.tasklist;
                    targetListConfig.cardData = cardData;
                    targetListConfig.cardTpl = result.data.taskcard.templets; */

                    let cardRows = this.tableCardDataHandle(null, result.data.taskcard, result.data.tasklist);
                    targetListConfig.firstPageRowData = {
                        rows: result.data.tasklist.rows,  //注意tasklist中要包含pageinfo信息，否则分页不对
                        allpks: this.currentTabPkList,
                        pageInfo: {
                            total: +this.currentTabPkList.length,
                            pageSize: +this.queryParam.pageinfo.size,
                            totalPage: +result.data.tasklist.pageinfo.totalPages,
                            pageIndex: 0
                        }
                    };
                    targetListConfig.cardData.cardRows = cardRows;
                    targetListConfig.cardData.listRows = result.data.tasklist.rows
                    targetListConfig.cardTpl = result.data.taskcard.templets;


                    this.setState({
                        listConfigArr
                    })

                }
            }
        },
        insertbefore: (isInsertBefore) => {
            this.setState({
                isInsertBefore: isInsertBefore
            })
        },
        autoopen: (isAutoOpenBillAfterQueryBarCode) => {
            this.setState({
                isAutoOpenBillAfterQueryBarCode
            })
        },
        fuzzySearch: {
            queryParam: this.queryParam
        }
    }
}